package com.k2data.k2app.controller;

import com.k2data.k2app.domain.po.v1.Contacts;
import com.k2data.k2app.mapper.ContactsMapper;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.ResponseHelper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author lidong9144@163.com 17-3-20.
 */
@RestController
@RequestMapping("/contacts")
public class ContactController {

    @Autowired
    private ContactsMapper contactsMapper;

    @ApiOperation(value = "添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contacts", value = "contacts", required = true, dataType = "Contacts", paramType = "body")
    })
    @PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> post(@Valid @RequestBody Contacts contacts) throws Exception {
        return ResponseHelper.success(contactsMapper.insert(contacts));
    }
    @ApiOperation(value = "更新")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "contacts", value = "contacts", required = true, dataType = "Contacts", paramType = "body")
    })
    @PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> put(@PathVariable("id") Long id, @RequestBody Contacts contacts) throws Exception {
        contacts.setId(id);
        return ResponseHelper.success(contactsMapper.updateByPrimaryKey(contacts));
    }
    @ApiOperation(value = "根据id获取")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping(value = "/{id}")
    public ResponseEntity<CommonResultResponse> get(@PathVariable("id") Long id) throws Exception {
        return ResponseHelper.success(contactsMapper.selectByPrimaryKey(id));
    }
}
